define(function(require, exports){
	var $ = require('jquery');
	var pubjs = require('pubjs');
	var util = require('util');
	var input  = require('@base/common/input');
	var dspUtil = require('dspUtil');

	var Client = input.subLevelCheckbox.extend({
		init: function(config, parent){
			config = pubjs.conf(0, config, {
				'class': 'M-commonSLC M-countryClient',
				'data': [
					{key: 'os', text: LANG('操作系统'), items: pubjs.config("client/os")}
					,{key: 'bs', text: LANG('浏览器'), items: pubjs.config("client/browser")}
					,{key: 'ls', text: LANG('系统语言'), items: pubjs.config("client/campaignLang"),config: {
							tips: LANG('暂只支持谷歌、谷歌移动渠道')
						}}
				],
				'all_value': ['ls']
			});

			this.Super('init', arguments);
			this.initList();
			// 反选按钮与清空按钮
			var buttonsDiv = $('<div class="M-commonSLCButtons"></div>');
			$('<a class="selectInverse">'+LANG('反选')+'</a>').appendTo(buttonsDiv);
			this.uiProxy(buttonsDiv, '.selectInverse', 'click', 'eventSelectInverse');
			$('<a class="clearAll">'+LANG('清空')+'</a>').appendTo(buttonsDiv);
			this.uiProxy(buttonsDiv, '.clearAll', 'click', 'eventClearAll');
			this.$el.prepend(buttonsDiv);
		},
		initList: function(){
			// 整理数据
			var t = [];
			var data = this.getConfig('data');
			util.each(data, function(value) {
				t.push(dspUtil._convertArrayGroup(value.text, value.key, value.items, value.config))
			});

			this.setList([{name: LANG('全选'), child: t}]);
		},
		eventSelectInverse: function() {
			var self = this;
			// 定义反选的数据data
			var inverseData = {};
			var data = self.getConfig('data');
			util.each(data, function(value) {
				var arr = self._getInverseArray(value);
				if(arr.length) {
					inverseData[value.key] = arr;
				}
			});

			this.setData(inverseData);
		},
		// 该方法能得到某一项(os/browser)的反选数组
		// 参数为该项的名字：os/browser
		// 返回对应的反选数组，注意在getData中空数组指代全选，而这里空数组则指代空
		_getInverseArray: function(value) {
			var wholeArray = [];
			util.each(value.items, function(item){
				if(item && item.list){
					var list = item.list;
					util.each(list, function(key, index){
						var id = index;
						if(util.isPlainObject(key)){
							id = key.id;
						}
						wholeArray.push(+id);
					});
				}
			});

			var inverseArray;
			// 。。。因为browser在data里使用缩写bs，所以browser的情况需先将name改为bs
			var selectedArray = this.getData()[value.key];
			if (!selectedArray) {
				// 没有选择的情况
				inverseArray = wholeArray.slice(0);
			} else if (selectedArray.length === 0) {
				// 长度为0，即全选的情况
				inverseArray = [];
			} else {
				// 取补集
				inverseArray = dspUtil._complementArray(wholeArray, selectedArray);
			}

			return inverseArray;
		},
		eventClearAll: function() {
			this.setData({});
		},
		/**
		 * 设置选中的项目
		 * @param {object} data 选中的项目ID值
		 * @return {None} 无返回
		 */
		setData: function(data){
			var all_value = this.getConfig('all_value');
			var sels = [];
			util.each(data, function(list, type){
				if (!~all_value.indexOf(type) && (!list || !list.length)){
					sels.push(type);
				}else {
					util.each(list, function(id){
						sels.push(type + ':' + id);
					});
				}
			});
			this.Super("setData",[sels]);
		},

		/**
		 * 获取选中的区域数据
		 * @return {Array} 返回选中的区域ID数组
		 */
		getData: function(){
			//执行父类的getData
			var ret = this.Super("getData");
			var all_value = this.getConfig('all_value');

			var data={}, all={};
			util.each(ret, function(id){
				var val = id.split(':');
				var type = val.shift();
				if (val.length === 0 && !~all_value.indexOf(type)){
					if (type){
						all[type] = true;
					}
				}else {
					if (val.length === 1){
						val = isNaN(+val[0]) ? val[0] : +val[0];
					}else {
						val = val.join(':');
					}
					if (!data[type]){
						data[type] = [val];
					}else {
						data[type].push(val);
					}
				}
			});
			util.each(data, function(list, type){
				if (all[type]){
					return [];
				}
			});
			return data;
		},

		eventHoverItem: function(){
			// 重置鼠标回调事件
		}
	});
	exports.base = Client;
});
